package com.li.core.service.system.impl;

import com.li.configserve.config.syslog.SysLog;
import com.li.configserve.domain.common.CurrentUser;
import com.li.configserve.domain.gloabreturn.ResponseInfo;
import com.li.configserve.domain.jwt.Audience;
import com.li.configserve.utils.jwt.JwtTokenUtils;
import com.li.core.domain.common.StrConstant;
import com.li.core.domain.system.LoginLogDO;
import com.li.core.domain.system.User;
import com.li.core.mapper.system.LoginLogMapper;
import com.li.core.mapper.system.UserMapper;
import com.li.core.service.system.SystemService;
import com.li.toolkit.utils.PasswordUtils;
import com.li.toolkit.utils.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: LiDong
 * @time: 2020/7/22 21:58
 **/
@Service
public class SystemServiceImpl implements SystemService {

    private static final Logger logger = LoggerFactory.getLogger(SystemServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private Audience audience;

    /**
     * 系统的登录接口
     *
     * @param request
     * @param paramMap
     * @return
     */
    @Override
    @SysLog
    public ResponseInfo<Map<String, Object>> login(HttpServletRequest request, Map<String, String> paramMap) {
        ResponseInfo<Map<String, Object>> responseInfo = new ResponseInfo<>();
        try {
            String password = paramMap.getOrDefault("password", "");
            String enPassword = PasswordUtils.desEncrypt(password);
            paramMap.put("password", enPassword);
            User user = userMapper.selectByMap(paramMap);
            if (user == null) {
                responseInfo.setMessage("请确认账户和密码信息！");
                responseInfo.setCode(StrConstant.ERROR);
                return responseInfo;
            }
            responseInfo.setCode(StrConstant.SUCCESS);
            CurrentUser currentUser = new CurrentUser();
            BeanUtils.copyProperties(user, currentUser);
            request.getSession().setAttribute("user", currentUser);
            String token = JwtTokenUtils.createJwt(String.valueOf(currentUser.getId()), currentUser.getUsername(), currentUser.getUsercode(), audience);
            responseInfo.setMessage("登录成功！");
            Map<String, Object> response = new HashMap<>();
            response.put("token", token);
            response.put("user", currentUser);
            responseInfo.setData(response);
            LoginLogDO loginLogDO = new LoginLogDO(UuidUtils.randomUuid(), String.valueOf(currentUser.getId()), "北京亦庄", new Date(), paramMap.get("ip"));
            insertLoginInfo(loginLogDO);
        } catch (Exception e) {
            responseInfo.setCode(StrConstant.NO_LOGIN);
            responseInfo.setMessage(e.getMessage());
            logger.error(e.getMessage());
        }
        return responseInfo;
    }

    /**
     * 插入登录日志
     *
     * @param loginLogDO
     */
    private void insertLoginInfo(LoginLogDO loginLogDO) {
        try {
            loginLogMapper.insert(loginLogDO);
        } catch (Exception e) {
            logger.error("error occur:{0}", e);
        }

    }


}
